package com.gimi.cloud.bbp.manage.service.app.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.Page;
import com.gimi.cloud.bbp.commonService.common.JmOperationLogService;
import com.gimi.cloud.bbp.dao.dao.homePage.MajorAdDao;
import com.gimi.cloud.bbp.dto.app.MajorAdDTO;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.entity.app.MajorAdEntity;
import com.gimi.cloud.bbp.manage.service.app.MajorAdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @auther loongs
 * @date 2017/12/29  13:13
 */
@Service
public class MajorAdServiceImpl implements MajorAdService {

    private MajorAdDao majorAdDao;
    private JmOperationLogService jmOperationLogService;
    @Autowired
    public MajorAdServiceImpl(MajorAdDao majorAdDao, JmOperationLogService jmOperationLogService) {
        this.majorAdDao = majorAdDao;
        this.jmOperationLogService=jmOperationLogService;
    }

    @Override
    public Page<MajorAdDTO> getMajorAdList(MajorAdDTO majorAdDTO) {
        PageHelper.startPage(majorAdDTO.getCurrentPage(), majorAdDTO.getPageSize());
        List<MajorAdDTO> result=majorAdDao.queryPageList(majorAdDTO);
        Page<MajorAdDTO> page =new Page(new PageInfo<>(result));
        return page;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean addMajorAd(MajorAdDTO majorAdDTO) {
        MajorAdEntity majorAdEntity = majorAdDao.queryBy("ad_name", majorAdDTO.getAdName());
        if(majorAdEntity != null){
            throw new MsgException(ResponseCodeEnum.MAJORAD_IS_EXIT);
        }
        MajorAdEntity majorAdEntityTemp=new MajorAdEntity();
        BeanUtils.copyProperties(majorAdDTO,majorAdEntityTemp);
        //初始化的广告位待发布状态
        majorAdEntityTemp.setAdStatus("c");
        Integer flag=majorAdDao.insertSelective(majorAdEntityTemp);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(majorAdDTO,baseDTO);
            jmOperationLogService.insertAddLog(baseDTO,majorAdDTO.getId()+"","addMajorAd","jimi_major_ad","添加首页广告");
            return true;
        }
        return false;
    }

    @Override
    public MajorAdEntity gotoEditMajorAd(MajorAdDTO majorAdDTO) {
        MajorAdEntity majorAdEntity= majorAdDao.selectByPrimaryKey(majorAdDTO.getId());
        //添加操作日志
        jmOperationLogService.insertQueryLog(new BaseDTO(),"gotoEditMajorAd","jimi_major_ad",majorAdDTO.getId()+"","转跳到指定首页广告界面");
        return majorAdEntity;
    }

    @Override
    public boolean editMajorAd(MajorAdDTO majorAdDTO) {
        //校验，如果已存在的广告名不能再添加或者修改成为此广告
        MajorAdEntity majorAdEntityTemp= majorAdDao.queryBy("ad_name", majorAdDTO.getAdName());
        if(majorAdEntityTemp != null && majorAdEntityTemp.getId().intValue() != majorAdDTO.getId().intValue()){
            throw new MsgException(ResponseCodeEnum.MAJORAD_IS_EXIT);
        }

        MajorAdEntity majorAdEntity=new MajorAdEntity();
        BeanUtils.copyProperties(majorAdDTO,majorAdEntity);
        majorAdEntity.setModifyTime(new Date());
        Integer flag=majorAdDao.updateByPrimaryKey(majorAdEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(majorAdDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"editMajorAd","jimi_major_ad",majorAdEntity.getId()+"","编辑指定首页广告");
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteMajorAd(MajorAdDTO majorAdDTO) throws MsgException {
        MajorAdEntity majorAdEntity= majorAdDao.selectByPrimaryKey(majorAdDTO.getId());
        if(majorAdEntity == null){
            throw new MsgException(ResponseCodeEnum.MAJORAD_IS_NOT_EXIT);
        }
        Integer flag=majorAdDao.deleteByPrimaryKey(majorAdEntity);
        if(flag>0){
            //添加操作日志
            jmOperationLogService.insertDeleteLog(majorAdDTO.getAddUid(),"deleteMajorAd","jimi_major_ad",majorAdDTO.getId()+"","删除指定首页广告");
            return true;
        }
        return false;
    }

    @Override
    public boolean underMajorAd(MajorAdDTO majorAdDTO) throws MsgException {
        MajorAdEntity majorAdEntity= majorAdDao.selectByPrimaryKey(majorAdDTO.getId());
        //处于已发布的主页广告才能撤下
        if(majorAdEntity == null || !"d".equals(majorAdEntity.getAdStatus())){
            throw new MsgException(ResponseCodeEnum.MAJORAD_IS_NOT_EXIT_OR_PUBLISH);
        }
        majorAdEntity.setAdStatus("e");
        majorAdEntity.setModifyTime(new Date());
        Integer flag=majorAdDao.updateByPrimaryKey(majorAdEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(majorAdDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"underMajorAd","jimi_major_ad",majorAdDTO.getId()+"","撤下首页广告");
            return true;
        }
        return false;
    }

    @Override
    public boolean publishMajorAd(MajorAdDTO majorAdDTO) throws MsgException {
        MajorAdEntity majorAdEntity= majorAdDao.selectByPrimaryKey(majorAdDTO.getId());
        //处于待发布和撤下的广告可以发布
        if(majorAdEntity == null || (!"c".equals(majorAdEntity.getAdStatus()) &&  !"e".equals(majorAdEntity.getAdStatus()))){
            throw new MsgException(ResponseCodeEnum.MAJORAD_IS_NOT_EXIT_OR_LOAODING_OR_UNDER);
        }
        majorAdEntity.setAdStatus("d");
        majorAdEntity.setPublishMan(majorAdDTO.getAddUid());
        majorAdEntity.setPublishTime(majorAdDTO.getPublishTime());
        majorAdEntity.setEndTime(majorAdDTO.getEndTime());
        majorAdEntity.setModifyTime(new Date());
        Integer flag=majorAdDao.updateByPrimaryKey(majorAdEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(majorAdDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"publishMajorAd","jimi_major_ad",majorAdDTO.getId()+"","发布首页广告");
            return true;
        }
        return false;
    }
}
